import {Component, OnInit} from '@angular/core';
import {FileManagementService} from '../file-management.service';
import {environment} from '../../../../environments/environment';
import {CommonTool} from '../../../common/common.tool';
import {NzModalService} from 'ng-zorro-antd';

@Component({
  selector: 'app-safety-chain-management',
  templateUrl: './safety-chain-management.component.html',
  styleUrls: ['./safety-chain-management.component.scss']
})
export class SafetyChainManagementComponent implements OnInit {

  public tableModel: Array<any>;
  public current = 1;
  public size;
  public total;
  public environment = environment;
  public isCreateVisible = false;
  public formInfo = {
    id: '',
    name: '',
    url: ''
  };

  constructor(public fileManagementService: FileManagementService,
              public commonTool: CommonTool,
              private modalService: NzModalService) {
    this.tableModel = [];
    this.size = environment.config.sizeOptions[0];
  }

  ngOnInit() {
    this.getData();
  }

  /**
   * 查询数据
   */
  public getData(data?) {
    this.fileManagementService.getSafetyChainPaging(this.size, this.current, data).subscribe(res => {
      this.tableModel = res.resData;
      this.total = res.total;
    });
  }

  /**
   * 修改current
   */
  public indexChange(e) {
    this.current = e;
    this.getData();
  }

  /**
   * 修改size
   */
  public sizeChange(e) {
    this.size = e;
    this.getData();
  }

  public sort(e) {
    if (e.value === 'ascend') {
      this.getData({sortFields: [e.key], rule: 'ASC'});
    } else if (e.value === 'descend') {
      this.getData({sortFields: [e.key], rule: 'desc'});
    } else {
      this.getData();
    }
  }

  /**
   * 提交
   */
  public createOk(formInfo) {
    if (this.commonTool.notNull(formInfo.id)) {
      this.fileManagementService.updateSafetyChain(formInfo.id, formInfo).subscribe(res => {
        this.modalService.success({
          nzTitle: '更新安全链接',
          nzContent: '操作成功！',
          nzOnOk: () => {
            this.isCreateVisible = false;
            this.formInfo = {
              id: '',
              name: '',
              url: ''
            };
            this.getData();
          }
        });
      });
    } else {
      this.fileManagementService.createSafetyChain(formInfo).subscribe(res => {
        this.modalService.success({
          nzTitle: '创建安全链接',
          nzContent: '操作成功！',
          nzOnOk: () => {
            this.isCreateVisible = false;
            this.formInfo = {
              id: '',
              name: '',
              url: ''
            };
            this.getData();
          }
        });
      });
    }
  }

  public create(data) {
    this.isCreateVisible = true;
    this.formInfo = JSON.parse(JSON.stringify(data));
  }

  public delete(data) {
    this.modalService.confirm({
      nzTitle: '确实要删除吗?',
      nzContent: '<b style="color: red;">删除后将无法恢复！</b>',
      nzOkText: '确定',
      nzOnOk: () => {
        this.fileManagementService.deleteSafetyChain(data.id).subscribe(res => {
          this.modalService.success({
            nzTitle: '删除安全链接',
            nzContent: '操作成功！',
            nzOnOk: () => {
              this.isCreateVisible = false;
              this.formInfo = {
                id: '',
                name: '',
                url: ''
              };
              this.getData();
            }
          });
        });
      },
      nzCancelText: '取消'
    });
  }
}
